package com.google.code.mergeinformer.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * Use @Configurable and DI instead
 * @author Dmitry Golubev
 */
@Component
@Deprecated
public class ServiceLocator {

    @Autowired
    private ApplicationContext nonStaticAppContext;

    private static volatile ApplicationContext applicationContext;

    private ServiceLocator() {
    }

    @PostConstruct
    private void captureApplicationContext() {
        applicationContext = nonStaticAppContext;
    }

    public static <T> T getService(Class<T> serviceInterface) {
        return applicationContext.getBean(serviceInterface);
    }

    public static <T> T getService(String name, Class<T> serviceInterface) {
        return applicationContext.getBean(name, serviceInterface);
    }
}
